package com.example.zcl.web;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.zcl.system.entity.House;
import com.example.zcl.system.result.Result;
import com.example.zcl.system.result.ResultUtil;
import com.example.zcl.system.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @desc: --
 * @author: Bing
 * @date: 2025-05-29 15:18
 */
@RestController
@RequestMapping("/api/house")
public class HouseController {

    @Autowired
    private HouseService houseService;

    @GetMapping("/list")
    public Result getAllHouses() {
        QueryWrapper<House> qw = new QueryWrapper<>();
        qw.orderByDesc("create_time");
        List<House> list = houseService.list();
        return ResultUtil.success(list);
    }

    @PostMapping
    public Result createHouse(@RequestBody House house) {
        house.setCreateTime(DateUtil.now());
        houseService.save(house);
        return ResultUtil.success();
    }

    @GetMapping("/{housename}")
    public Result getHouseByHousename(@PathVariable String housename) {
        houseService.getByHousename(housename);
        return ResultUtil.success();
    }

    @DeleteMapping("/{id}")
    public Result deleteHouse(@PathVariable Long id) {
        houseService.removeById(id);
        return ResultUtil.success();
    }

    @PutMapping("/{id}")
    public Result updateHouse(@PathVariable Long id, @RequestBody House house) {
        house.setId(id);
        houseService.updateById(house);
        return ResultUtil.success();
    }
}
